﻿using System;
using System.Collections;
using NHibernate;
using NHibernate.Cfg;
using System.Collections.Generic;

namespace Business
{
    public class PartnerLabourContract
    {
        private ISessionFactory _sessionFactory;
        public PartnerLabourContract()
        {
            _sessionFactory = DataObjects.Service.SessionFactory.GetInstance().GetNHibernateFactory();
        }
        
        public ArrayList List()
        {
            ArrayList arrRet = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                arrRet = (ArrayList)session.CreateCriteria(typeof(DataObjects.PartnerLabourContract)).List();
                tx.Commit();
            }
            return arrRet;
        }

        public ArrayList GetListByPartnerId(string Id)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select * from PARTNER_LABOUR_CONTRACT where PARTNER_ID = ? order by EFFECTIVE_DATE desc").AddEntity(typeof(DataObjects.PartnerLabourContract));
                query.SetString(0, Id);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                return arrTemp;
            }
        }

        public DataObjects.PartnerLabourContract GetObjectByDate(string partner_id, DateTime value_date)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from PARTNER_LABOUR_CONTRACT where PARTNER_ID = ? and EFFECTIVE_DATE <= '" + value_date.ToString("yyyy-MM-dd") + "' and END_DATE >= '" + value_date.ToString("yyyy-MM-dd") + "' order by EFFECTIVE_DATE desc").AddEntity(typeof(DataObjects.PartnerLabourContract));
                query.SetString(0, partner_id);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.PartnerLabourContract)arrTemp[0];
                }
                return null;
            }
        }

        public DataObjects.PartnerLabourContract GetObjectByPK(string contractno)
        {
            ArrayList arrTemp = new ArrayList();
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                IQuery query = session.CreateSQLQuery("select top 1 * from PARTNER_LABOUR_CONTRACT where contract_no =?").AddEntity(typeof(DataObjects.PartnerLabourContract));
                query.SetString(0, contractno);
                arrTemp = (ArrayList)query.List();
                tx.Commit();

                if (arrTemp.Count > 0)
                {
                    return (DataObjects.PartnerLabourContract)arrTemp[0];
                }
                return null;
            }
        }

        //code insert - update - delete 
        public void Insert(DataObjects.PartnerLabourContract oPLC)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Save(oPLC);
                tx.Commit();			        
            }
        }
        public void Update(DataObjects.PartnerLabourContract oPLC)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                session.Update(oPLC);
                tx.Commit();		        
            }
        }
        public void DeleteByPK(string contractno)
        {
            using (ISession session = _sessionFactory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                DataObjects.PartnerLabourContract oPLC = (DataObjects.PartnerLabourContract)session.Load(typeof(DataObjects.PartnerLabourContract), contractno);
                session.Delete(oPLC);
                tx.Commit();
            }
        }
    }
}
